package studio.xiaoyun.core.dao.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import studio.xiaoyun.core.dao.IFeedbackDao;
import studio.xiaoyun.core.entity.Feedback;
import studio.xiaoyun.core.parameter.FeedbackParameter;

@Repository("feedbackDao")
public class FeedbackDao extends BaseDao<Feedback,Long> implements IFeedbackDao {

	@Override
	public void deleteByID(List<Long> IDs) {
		if(IDs==null || IDs.isEmpty()) return;
		String sql = "delete from feedback where feedbackID in ("+IDs.toString().replaceAll("\\[|\\]", "")+")";
		Session session = this.getSession();
		SQLQuery query = session.createSQLQuery(sql);
	    query.executeUpdate();
	}

	@Override
	public Feedback getByID(Long ID) {
		Session session = this.getSession();
		return session.get(Feedback.class, ID);
	}

	@Override
	public Feedback loadByID(Long ID) {
		Session session = this.getSession();
		return session.load(Feedback.class, ID);
	}

	@Override
	public void delete(List<Feedback> entitys) {
		List<Long> list = new LinkedList<Long>();
		for(Feedback feedback:entitys){
			list.add(feedback.getFeedbackID());
		}
		this.deleteByID(list);
	}

	@Override
	public long getFeedbackCountByParameter(FeedbackParameter feedbackParameter) {
		String sql = this.buildSqlForCount(null, null, feedbackParameter);
		long count = this.getCountBySQL(sql);
		return count;
	}

	@Override
	public List<Feedback> getFeedbacksByParameter(FeedbackParameter feedbackParameter) {
		String sql = this.buildSqlForList(null, null, feedbackParameter);
		List<Feedback> list = this.getBySQL(sql, "feedback_0", Feedback.class);
		return list;
	}
	
	protected String buildSqlForCount(String table,String where,FeedbackParameter feedbackParameter){
		if(feedbackParameter==null){
			feedbackParameter = new FeedbackParameter();
		}
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from feedback as feedback_0 ");
		sql.append(table==null?"":table);
		sql.append(" where 1=1");
		if(where!=null && where.trim().length()>0){
			sql.append(" and ").append(where);
		}
		String whereStr = feedbackParameter.getQuery(this.getMapping());
		if(whereStr!=null && whereStr.trim().length()>0){
			sql.append(" and ").append(whereStr);
		}
		return sql.toString();
	}
	
	protected String buildSqlForList(String table,String where,FeedbackParameter param){
		if(param==null){
			param = new FeedbackParameter();
		}
		StringBuffer sql = new StringBuffer();
		Map<String,String> mapping = this.getMapping();
		sql.append("select feedback_0.* from feedback feedback_0 ");
		sql.append(table==null?"":table);
		sql.append(" where 1=1");
		if(where!=null && where.trim().length()>0){
			sql.append(" and ").append(where);
		}
		String whereStr = param.getQuery(mapping);
		if(whereStr!=null && whereStr.trim().length()>0){
			sql.append(" and ").append(whereStr);
		}
		List<String> sortFields = param.getSortField();
		List<Boolean> isAscs = param.getIsAsc();
		StringBuffer orderStr = new StringBuffer();
		for(int i=0;i<sortFields.size();i++){
			orderStr.append(mapping.get(sortFields.get(i))).append(isAscs.get(i)?" asc ,":" desc ,");
		}
		if(orderStr.length()>0){
			sql.append(" order by ").append(orderStr.delete(orderStr.length()-2,orderStr.length()));
		}
		sql.append(" limit ").append(param.getFirstResult()).append(",").append(param.getMaxResults());
		return sql.toString();
	}
	
	/**
	 * 
	 * @return 搜索类中属性名和数据表中列名的映射关系
	 */
	protected Map<String,String> getMapping(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("feedbackID", "feedback_0.feedbackID");
		map.put("title", "feedback_0.title");
		map.put("text", "feedback_0.text");
		map.put("createDate", "feedback_0.createDate");
		return map;
	}

}
